﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Translate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                
                if ((ou.UserAccessLevel != ("5")) && (ou.UserAccessLevel != ("3")))
                {
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=" + ou.UserAccessLevel);
                }
                
                if (ou.TeamId == "Null")
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=274ggdizz5!lk");

                string Language = GND.ReadNivdalIntData("UserId", "Nivdal_GetUserTranslateLanguage", ou.UserId);
                if ((Language == null) || (Language == "0"))
                    Response.Redirect("GeneralMessage.aspx?MessageString=k882m299nnqmhtsd");
                else if (Language == "2")
                    Response.Redirect("TranslateEnglish.aspx");
                else
                {
                    this.LanguageHiddenField.Value = Language;
                    if (!IsPostBack)
                    {
                        TranslateDataSource.SelectParameters.Add("UserId", ou.UserId);
                        TranslationStatusDataSourse.SelectParameters.Add("Language", Language);
                    }
                }
                
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void SubmitButtonClick(object sender, EventArgs e)
        {
            string TranslateText;
            string SentenceNumber;
            string SentencePart;
            string Language = LanguageHiddenField.Value;
            string Answer = " ";
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            foreach (ListViewItem Item in TranslateListView.Items)
            {
                TranslateText = (Item.FindControl("TranslateTextBox") as TextBox).Text.Trim();
                SentenceNumber = (Item.FindControl("SentenceNumHiddenField") as HiddenField).Value;
                SentencePart = (Item.FindControl("PartHiddenField") as HiddenField).Value;

                Answer += GND.ReadNivdalStringData("SentenceNum", "Part", "Language", "Text", "Nivdal_TranslateSentencePart",
                    SentenceNumber, SentencePart, Language, TranslateText);
                Answer += "<br />";

            }
            MessageLabel.Text = Answer;
            TranslateListView.DataBind();
            TranslationStatusForumView.DataBind();
        }

        protected void SkipSentence(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string SentenceNum = (this.TranslateListView.Items[0].FindControl("SentenceNumHiddenField") as HiddenField).Value;
            string Answer = GND.ReadNivdalStringData("UserId", "SentenceNum", "Nivdal_SkipSentenceTranslation", ou.UserId, SentenceNum);
            MessageLabel.Text = Answer;
            TranslateListView.DataBind();
            TranslationStatusForumView.DataBind();
        }

        protected string StatusLabelString(object Num, object OutOf)
        {
            int NumInt = Int32.Parse(Num.ToString());
            int OutOfInt = Int32.Parse(OutOf.ToString());

            string result = NumInt.ToString() + " " +
                            Resources.Resource.Translate_StatusOutOfLabelText + " " +
                            OutOfInt.ToString() + " (" + ((NumInt * 100) / OutOfInt).ToString() + "%)";
            return (result);
        }

        protected string TranslatorString(object LanguageName)
        {
            return (Resources.Resource.Translate_YouTranslateToLabelText + " " + LanguageName.ToString());
        }
    }
}